package com.wl.application.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wl.application.common.all.Const;
import com.wl.application.common.all.Result;
import com.wl.application.common.all.SysLog;
import com.wl.application.common.childException.BusinessException;
import com.wl.application.entity.Menu;
import com.wl.application.services.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/22
 * @description:
 */
@RestController
@RequestMapping("/menu")
@CrossOrigin
public class MenuController {

    @Autowired
    private MenuService menuService;
    @GetMapping("/menuTree")
    public Result menuTree() {
        return Result.ok(menuService.nodeList());
    }


    @GetMapping("/list")
    public Result list() {
        return Result.ok(menuService.list());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Menu menu) {
        menu.setCreateTime(new Date());
        menu.setMstate(Const.NO_ACTIVATE);
        return Result.ok("添加成功!", (menuService.save(menu)));
    }

    @PutMapping("/update/state/{id}")
    public Result updateState(@PathVariable("id") Integer id) {
        return Result.ok(menuService.updateState(id));
    }



    @PutMapping("/update")
    public Result update(@RequestBody Menu menu) {
        return Result.ok("修改成功!", menuService.updateById(menu));
    }

    @DeleteMapping("/del")
    public Result delete(@RequestBody List<Integer> list) {
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.in("pid",list);
        List list1 = menuService.list(wrapper);
        if (list1.size()!= 0) {
            throw  new BusinessException(2,"请先删除子菜单!");
        }
        return Result.ok("删除成功!", menuService.removeBatchByIds(list));
    }

    @GetMapping("/one/{id}")
    public Result getOne(@PathVariable Integer id) {
        return Result.ok(menuService.getMenuById(id));
    }

    @PutMapping("/updatePid/{id}/{pid}")
    public Result updatePid(@PathVariable("id") Integer id,@PathVariable("pid")Integer pid){
        Menu menu = new Menu();
        menu.setPid(pid);
        menu.setId(id);
        boolean b = menuService.updateById(menu);
        return Result.ok(b);
    }
}
